<?php
Class Joke{
	public static function addJoke($joke,$user){
		$result = mysql_query("INSERT INTO ".DATABASE_TABLE_JOKE."(date,joke,id_user) 
						VALUES('".time()."','".mysql_escape_string($_POST['joke'])."','".$user->id."'  )");
		if(! $result){
			return false;
		}else{
			return true;
		}
	}
	public static function getNumberJoke(){
		return mysql_num_rows(mysql_query("SELECT id FROM ".DATABASE_TABLE_JOKE));
	}

	public static function userVote($idjoke,$user){
		$idjoke=mysql_escape_string($idjoke);
		//$user=mysql_escape_string($user);
		$nb = mysql_num_rows(mysql_query("SELECT id FROM ".DATABASE_TABLE_VOTE." WHERE id_user like '".$user->id."' AND id_joke like '$idjoke' "));
		if($nb >= 1){
			return true;
		}
		return false;
	}
	
	public static function voteJoke($idjoke,$vote,$user){
		$idjoke=mysql_escape_string($idjoke);
		if(($vote=='1' or $vote=='-1') and Joke::userVote($idjoke,$user) == false ){
			mysql_query("INSERT INTO ".DATABASE_TABLE_VOTE."(id_joke,id_user,vote) VALUES('$idjoke',".$user->id.",'$vote') ") ;
			mysql_query("UPDATE ".DATABASE_TABLE_JOKE." SET score = score+(".$vote.") WHERE id ='$idjoke' ;");
			return true;
		}
		return false;
	}
}
?>